<?php

/**
 * 友情链接
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: FlinkController.class.php v1.0 2014-06-23 17:27:03 $
 */
namespace Admin\Controller;
class FlinkController extends CommonController{
    private $model;
    private $service;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Flink");
        $this->service = D("Flink", "Service");
    }
    
    /**
     * 列表
     */
    public function index() {
        $result = $this->getPagination('Flink');
        //var_dump($result);die;
        $this->assign('list', $result['data']);
        $this->assign('count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }
    
    /**
     * 添加
     */
    public function add() {
        $this->display();
    }
    
    /**
     * 创建
     * @return type
     */
    public function create(){
        $flink = I("post.link", NULL);
        if(!isArrayAndNotEmpty($flink)){
            return $this->errorReturn("无效操作！");
        }
        
        $result = $this->service->add($flink);
        if(!$result["status"]){
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn("添加友情链接成功", U("index"));
    }
    
    /**
     * 修改
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            $this->error("无效操作");
        }
        
        $link = $this->service->getById($id);
        if(!isArrayAndNotEmpty($link)){
            $this->error("无效操作");
        }
        
        $this->assign($link);
        $this->display();
    }
    
    /**
     * 修改
     * @return type
     */
    public function update() {
        $flink = I("post.link", NULL);
        if(!isArrayAndNotEmpty($flink)){
            return $this->errorReturn("无效操作！");
        }
        
        $result = $this->service->update($flink);
        if(!$result["status"]){
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn("修改友情链接成功", U("index"));
    }
    /**
     * 删除
     */
    public function delete() {
        $act = I("post.act", "one");
        if($act == "one"){
            $id = I("post.fid", 0, "intval");
            if($id <= 0){
                return $this->errorReturn("无效操作！");
            }
            $flink = $this->service->getById($id);
            if(!isArrayAndNotEmpty($flink)){
                return $this->errorReturn("无效操作！");
            }
            if($this->service->delete($id)){
                return $this->successReturn("删除成功", U("index"));
            }else{
                return $this->errorReturn("删除失败！");
            }
        }else if($act == "two"){
            $fids = I("post.fids");
            if(!isArrayAndNotEmpty($fids)){
                return $this->errorReturn("无效操作！");
            }
            foreach ($fids as $fid) {
                $flink = $this->service->getById($fid);
                if(!isArrayAndNotEmpty($flink)){
                    return $this->errorReturn("您要删除的数据不存在！");
                }
                $this->service->delete($fid);
            }
            return $this->successReturn('删除数据成功！', U('index'));
        }else{
            return $this->errorReturn("无效操作！");
        }
    }
}
